package com.zhuge_new.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.arch.lifecycle.ViewModelProvider;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhuge_new.Adapter.ShareBottonAdapter;
import com.zhuge_new.Adapter.ShareTopAdapter;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.bean.ShareBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.wxapi.Constacts;

import java.io.File;


/**
 * Created by Administrator on 2018/11/2.
 */

public class ShareActivity extends BaseActivity implements View.OnClickListener {

    private String imgurl;
    private IWXAPI wxApi;
    private RecyclerView share_list;
    private RecyclerView share_top_list;
    private int seat=0;
    private ShareTopAdapter shareTopAdapter;
    private ShareBottonAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        init_title();
        getData();
    }

    @Override
    public void init_title() {
        super.init_title();
        ImageView iv_back=findViewById(R.id.iv_nav_back);
        ImageView iv_right=findViewById(R.id.iv_nav_right);
        TextView tv_title=findViewById(R.id.tv_nav_title);
        iv_right.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        iv_right.setOnClickListener(this);
        tv_title.setText("分享赚钱");
        wxApi = WXAPIFactory.createWXAPI(baseContext, Constacts.WXAPPID);
        wxApi.registerApp(Constacts.WXAPPID);
        share_list = findViewById(R.id.share_list);
        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        share_list.setLayoutManager(manager);

        LinearLayoutManager manager1=new LinearLayoutManager(this);
        manager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        share_top_list = findViewById(R.id.share_top_list);
        share_top_list.setLayoutManager(manager1);
    }


    public void getData(){
        OkGo.<String>post(BaseHttp.get_shareMakeMoneyList)
                .tag(this)
                .headers("token", PreferencesUtils.getString(baseContext,"token"))
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        final ShareBean shareBean= (ShareBean) new DataParse().getBean(response.body(),ShareBean.class,1);

                        adapter = new ShareBottonAdapter(baseContext,shareBean.getObject(),share_list);
                        share_list.setAdapter(adapter);
                        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapteraa, View view, int position) {
                                adapter.getSelectedPosition(position);
                                shareTopAdapter.getSelectedPosition(position);
                                for (int i=0;i<shareBean.getObject().getDataList().size();i++){
                                    if (i==position){
                                        shareBean.getObject().getDataList().get(position).setFlag(true);
                                    }else {
                                        shareBean.getObject().getDataList().get(i).setFlag(false);
                                    }
                                    adapter.notifyDataSetChanged();
                                }
                            }
                        });
                        shareTopAdapter = new ShareTopAdapter(baseContext,shareBean.getObject(),share_top_list);
                        share_top_list.setAdapter(shareTopAdapter);
                        shareTopAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapterbb, View view, int position) {
                                shareTopAdapter.getSelectedPosition(position);
                                adapter.getSelectedPosition(position);
                            }
                        });
                        share_top_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
                            @Override
                            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                                super.onScrolled(recyclerView, dx, dy);
                                int TopPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
                                adapter.getSelectedPosition(TopPosition);
                            }
                        });
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(ShareActivity.class);
                break;
            case R.id.iv_nav_right:

                break;
        }
    }


    //一个bitmap对象，一个发送位置
    public void imageShare(Bitmap bmp,int sendtype){

        WXImageObject imgObj = new WXImageObject();

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 1, 1, true);
        msg.setThumbImage(thumbBmp);
        bmp.recycle();
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = sendtype==0?SendMessageToWX.Req.WXSceneSession:SendMessageToWX.Req.WXSceneTimeline;

        wxApi.sendReq(req);
    }


}
